package org.jeecg.modules.columnTopic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.constant.RedisConstant;
import org.jeecg.constant.WXConstant;
import org.jeecg.modules.article.entity.Article;
import org.jeecg.modules.article.service.IArticleService;
import org.jeecg.modules.columnTopic.dto.TemplateData;
import org.jeecg.modules.columnTopic.entity.ColumnTopicPublish;
import org.jeecg.modules.columnTopic.mapper.ColumnTopicPublishMapper;
import org.jeecg.modules.columnTopic.service.IColumnTopicPublishService;
import org.jeecg.modules.columnTopic.vo.WxMessageVO;
import org.jeecg.modules.columnmanager.vo.ColumnManagerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 栏目订阅
 * @Author: jeecg-boot
 * @Date:   2020-12-05
 * @Version: V1.0
 */
@Service
public class ColumnTopicPublishServiceImpl extends ServiceImpl<ColumnTopicPublishMapper, ColumnTopicPublish> implements IColumnTopicPublishService {

    @Autowired
    private ColumnTopicPublishMapper columnTopicPublishMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IArticleService articleService;
    @Override
    public WxMessageVO packageInfo(Article article, String openId) {
        String articleAuthor = article.getArticleAuthor();
        Date createTime = article.getCreateTime();
        String articleTitle = article.getArticleTitle();
        String articleColumn = article.getArticleColumn();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String articleCreateTimeStr = sdf.format(createTime);

        // 封装信息
        WxMessageVO wxMessageVO = new WxMessageVO();
        wxMessageVO.setTouser(openId);
        wxMessageVO.setTemplate_id(WXConstant.WX_TOPIC_TEMP_ID);
        Map<String, TemplateData> map = new HashMap<>();
        map.put("thing1",new TemplateData(articleTitle));
        map.put("thing2",new TemplateData(articleAuthor));
        map.put("time3",new TemplateData(articleCreateTimeStr));
        map.put("thing4",new TemplateData(articleColumn));
        wxMessageVO.setData(map);
        return wxMessageVO;
    }

    @Override
    public List<HashMap<String, Object>> getTopicInfo(String openId) {
        List<HashMap<String, Object>> list = new ArrayList<>();
        List<ColumnManagerVO> infos = columnTopicPublishMapper.getColumnInfoByOpenId(openId);

        infos.forEach(info->{
            // 设置栏目图片
            info.setColumnImageString(info.getcolumnImageString());
            Object o = redisUtil.get(RedisConstant.ARTICLE_PUBLISH + info.getId());
            HashMap<String, Object> map = (HashMap<String, Object>) o;
            HashSet<String> set = (HashSet<String>) map.get(openId);
            // 封装信息
            HashMap<String, Object> res = new HashMap(2);
            res.put("columnInfo",info);
            res.put("newArticle",set);
            list.add(res);
        });

        return list;
    }

    @Override
    public String getColumnIdByName(String columnName) {

        return columnTopicPublishMapper.getColumnIdByName(columnName);
    }

    @Override
    public void deleteColumnTopicUser(String id) {
        columnTopicPublishMapper
                .delete(new LambdaQueryWrapper<ColumnTopicPublish>()
                .eq(ColumnTopicPublish::getColumnId,id));
    }
}
